package rsj.api.bil;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.TableNode;
import common.util.Assert;
import common.util.BoxUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.context.ApplicationContextUtil;
import rsj.service.BilService;
import rsj.sql.user.UserDao;
import rsj.util.BilQuery;
import rsj.util.PaginationData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class GetOrgBilList extends BaseLink {

    @Autowired
    private BilService bilService;

    /**
     * Start_Date
     * End_Date
     * Orgp_Ids
     * Org_Id
     * <p>
     * orgp_id,orgp_prod_name,orgp_prod_id
     *
     * @param in
     */
    @RequestMapping(value = "rsj_operative_bilList")
    public Box executeLink(Box in) {
        MapNode data = in.getMapNode("data");
        MapNode user = in.getMapNode("$user");
        String userId = user.getValue("User_Id");
        Assert.isNotBlank(userId, "用户Id不能为空！");
        BilQuery bilQuery = buildQuery(data, userId);

        PaginationData<TableNode> paginationData = bilService.getBilList(bilQuery);

        MapNode out = new MapNode();
        out.putNode("Data_List", paginationData.getDataList().toJson());
        out.putNode("Total_Size", String.valueOf(paginationData.getTotalCount()));

        return BoxUtil.successBoxToData(in, "账单列表！", out);
    }

    private BilQuery buildQuery(MapNode data, String userId) {
        Assert.notNull(data, "data不能为空");
        UserDao userDao = ApplicationContextUtil.getBean(UserDao.class);
        String orgId = userDao.queryOrgIdByUser(userId).getValue("User_Org_Id");

        Assert.isNotBlank(orgId, "机构id不能为空");

        MapNode page = data.getMapNode("page");
        Assert.isTrue(orgId.length() > 0 && StringUtils.isNotBlank(page.getValue("Page_Size"))
                && StringUtils.isNotBlank(page.getValue("Page_Now")), "分页不能为空");

        BilQuery bilQuery = new BilQuery();
        bilQuery.setStart_Date(data.getValue("Start_Date"));
        bilQuery.setEnd_Date(data.getValue("End_Date"));
        bilQuery.setOrg_Id(orgId);
        bilQuery.setOrgP_Ids(checkAndGetIds(data.getValue("Orgp_Ids")));

        bilQuery.setPage_Count(page.getValueInt("Page_Now"));
        bilQuery.setPage_Size(page.getValueInt("Page_Size"));
        return bilQuery;
    }

    private List<String> checkAndGetIds(String orgPIds) {
        List<String> orgPIdList = new ArrayList<>();
        if (StringUtils.isBlank(orgPIds)) {
            return orgPIdList;
        }

        String[] orgPIdArr = orgPIds.split(",");

        orgPIdList.addAll(Arrays.asList(orgPIdArr));

        return orgPIdList;
    }

    private void checkParams(MapNode data) {
        Assert.isNotBlank(data.getValue("Prod_Id"), "产品ID不能为空！");
        Assert.isNotBlank(data.getValue("Org_Id"), "机构Id不能为空！");
    }

}
